import sys
from PyQt6.QtWidgets import QApplication
from period_tracker.ui.main_window import MainWindow
from period_tracker.core import NotificationManager, User, PeriodPredictor
import threading
import time
import json
from datetime import datetime

def check_reminders(data_manager, notification_manager):
    while True:
        # 每天检查一次
        for file_path in data_manager.data_dir.glob('*.json'):
            with open(file_path, 'r', encoding='utf-8') as f:
                data = json.load(f)
                user = User()
                user.__dict__.update(data)
                
                predictor = PeriodPredictor(user)
                next_period = predictor.predict_next_period()
                
                if next_period:
                    days_until = (next_period - datetime.now()).days
                    if days_until in [3, 2, 1]:
                        notification_manager.send_period_reminder(user, days_until)
                        
        time.sleep(24 * 60 * 60)  # 休眠24小时

def main():
    app = QApplication(sys.argv)
    
    notification_manager = NotificationManager()
    notification_manager.start_wechat()
    
    window = MainWindow()
    window.show()
    
    # 启动提醒检查线程
    reminder_thread = threading.Thread(
        target=check_reminders,
        args=(window.data_manager, notification_manager),
        daemon=True
    )
    reminder_thread.start()
    
    sys.exit(app.exec())

if __name__ == '__main__':
    main() 